package cn.fl.ref.fribaseinfo.utils;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.preinsroute.vo.PreRouteInsResVO;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 金融机构名称修改工具类
 */
public class FundRasInsNmChangeUtil {
    private static final Logger logger = LoggerFactory.getLogger(FundRasInsNmChangeUtil.class);

    /**
     * 根据金融机构业务线,设置金融机构名称展示字段值
     * 格式:金融机构名称展示 = 金融机构名称 + (业务线字典)
     * @param friBaseInfoVOList 机构信息List
     * @param bussinessLineDicList 业务线字典List
     */
    public static void insNmShowAddBussinessLineDic(List<FriBaseInfoVO> friBaseInfoVOList,List<KeyValueVO> bussinessLineDicList) {
        BaseLogUtils.info(logger, "insNmShowAddBussinessLineDic", "设置金融机构名称展示字段值before", JSONObject.toJSONString(friBaseInfoVOList));
        BaseLogUtils.info(logger, "insNmShowAddBussinessLineDic", "业务线字典List", JSONObject.toJSONString(bussinessLineDicList));
        if (CollectionUtils.isNotEmpty(friBaseInfoVOList) && CollectionUtils.isNotEmpty(bussinessLineDicList)) {
            for (FriBaseInfoVO friBaseInfoVO : friBaseInfoVOList) {
                //设置fundRasInsNmShow默认值
                friBaseInfoVO.setFundRasInsNmShow(friBaseInfoVO.getFundRasInsNm());
                if (friBaseInfoVO.getBussinessLine() == null) {
                    //有些金融机构不存在业务线字段
                    continue;
                }
                for (KeyValueVO bussinessLineDicVO : bussinessLineDicList) {
                    if (StringUtils.isNotEmpty(friBaseInfoVO.getBussinessLine()) &&
                            StringUtils.isNotEmpty(bussinessLineDicVO.getKey()) &&
                            friBaseInfoVO.getBussinessLine().equals(bussinessLineDicVO.getKey())) {
                        String fundRasInsNm = friBaseInfoVO.getFundRasInsNm();
                        friBaseInfoVO.setFundRasInsNmShow(fundRasInsNm + "(" + bussinessLineDicVO.getValue() + ")");
                        break;
                    }
                }
            }
        }
        BaseLogUtils.info(logger, "insNmShowAddBussinessLineDic", "设置金融机构名称展示字段值after", JSONObject.toJSONString(friBaseInfoVOList));
    }

    /**
     * 通过机构ID获取机构添加业务线字典名称
     * @param friBaseInfoVOList 机构信息List
     * @param fundRasInsId 资方机构ID
     * @return
     */
    public static String getFundRasInsNmShow(List<FriBaseInfoVO> friBaseInfoVOList,String fundRasInsId){
        BaseLogUtils.info(logger, "friBaseInfoVOList", "通过机构ID获取机构添加业务线字典名称 friBaseInfoVOList", JSONObject.toJSONString(friBaseInfoVOList));
        String fundRasInsNmShow = "";
        if (CollectionUtils.isEmpty(friBaseInfoVOList) || StringUtils.isEmpty(fundRasInsId)) {
            return fundRasInsNmShow;
        }
        for (FriBaseInfoVO friBaseInfoVO : friBaseInfoVOList) {
            if (friBaseInfoVO.getId() != null && friBaseInfoVO.getId().longValue() == Long.parseLong(fundRasInsId)
                    && StringUtils.isNotEmpty(friBaseInfoVO.getFundRasInsNmShow())) {
                BaseLogUtils.info(logger, "friBaseInfoVOList", "通过机构ID获取机构添加业务线字典名称 机构ID", fundRasInsId);
                BaseLogUtils.info(logger, "friBaseInfoVOList", "通过机构ID获取机构添加业务线字典名称 机构名称", friBaseInfoVO.getFundRasInsNmShow());
                return friBaseInfoVO.getFundRasInsNmShow();
            }
        }
        return fundRasInsNmShow;
    }

    /**
     * 设置预筛选结果中,机构名称添加业务线字典
     * @param preRouteInsResVOList
     * @param friBaseInfoVOList
     */
    public static void insNmAddBussLineDicForPreRouteInfo(List<PreRouteInsResVO> preRouteInsResVOList,List<FriBaseInfoVO> friBaseInfoVOList) {
        BaseLogUtils.info(logger, "insNmAddBussLineDicForPreRouteInfo", "设置预筛选结果中机构名称添加业务线字典before", JSONObject.toJSONString(preRouteInsResVOList));
        BaseLogUtils.info(logger, "insNmAddBussLineDicForPreRouteInfo", "资管机构List", JSONObject.toJSONString(friBaseInfoVOList));
        if (CollectionUtils.isNotEmpty(friBaseInfoVOList) && CollectionUtils.isNotEmpty(preRouteInsResVOList)) {
            for (FriBaseInfoVO friBaseInfoVO : friBaseInfoVOList) {
                if (StringUtils.isNotEmpty(friBaseInfoVO.getId()) && StringUtils.isNotEmpty(friBaseInfoVO.getFundRasInsNmShow())) {
                    for (PreRouteInsResVO preRouteInsResVO : preRouteInsResVOList) {
                        if (StringUtils.isNotEmpty(preRouteInsResVO.getInsId()) && preRouteInsResVO.getInsId().longValue() == friBaseInfoVO.getId().longValue()) {
                            preRouteInsResVO.setInsNm(friBaseInfoVO.getFundRasInsNmShow());
                            break;
                        }
                    }
                }
            }
        }
        BaseLogUtils.info(logger, "insNmAddBussLineDicForPreRouteInfo", "设置预筛选结果中机构名称添加业务线字典after", JSONObject.toJSONString(preRouteInsResVOList));
    }

}
